import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const state = {
    groupAvatar: "",
    test: "0",
    avatarChangeSign: 0,
    newfriend: {
        imageurl: require('../assets/Images/image2.jpg'),
        nickname: "变成暗翼守护佐伊小姐",
        senderId: 0,
        isConform: 0//正在申请
    },
    contactlist: {
        newfriends: [
            //     {
            //     imageurl: require('../assets/Images/image2.jpg'),
            //     nickname: "变成暗翼守护佐伊小姐",
            //     motto: "我爱吃大白兔奶糖",
            //     remark: "亚托克斯",
            //     age: "正无穷",
            //     isConform:0//正在申请
            // },
        ],
        friends: [
            //     {
            //   imageurl: ('../../assets/Images/image2.jpg'),
            //   nickname: "变成暗翼守护佐伊小姐",
            //   motto: "我爱吃大白兔奶糖",
            //   remark: "亚托克斯",
            //   account: 1
            // }
        ],
        group: [
            // {
            //   imageurl: ("../../assets/Images/image5.jpg"),
            //   nickname: "清风",
            //   motto: "他强仍他强，清风伏山岗",
            //   remark: "亚托克斯",
            //   age: "正无穷",
            //   account: "1175939261"
            // }
        ],
        family: [
            // {
            //   imageurl: ("../../assets/Images/image5.jpg"),
            //   nickname: "曹操",
            //   motto: "春风不与周郎便，铜雀春深锁二乔",
            //   remark: "亚托克斯",
            //   age: "正无穷",
            //   account: "1175939261"
            // },
        ]
    },
    localPicture: {
        key1: "hh"
    },
    show_person_info: {
        account: "123",
        nickname: "阿瑞",
        motto: "我爱吃大白兔奶糖",
        remark: "段雅婷",
        group: "好友",
        imageurl: require("../assets/Images/image1.jpg"),
        index: 0
    },
    ownInfo: {},
    conversationList: [],
    lastMessageId: 0,
    currentConversationIndex: 0,
    images: [],
    selfAvatar: ''
}
export default new Vuex.Store({
    strict: process.env.NODE_ENV !== 'production',
    state,
    getters: {
        hasNewFriends(state) {
            for (let newfriend of state.contactlist.newfriends) {
                if (newfriend.isConform == 0) {
                    return true
                }
            }
            return false
        },
        currentConversation(state) {
            return state.conversationList[state.currentConversationIndex]
        }
        , lastMessage(state) {
            return (id) => {
                const conversationInfo = state.conversationList[id]
                if (conversationInfo.messages.length === 0) {
                    return ''
                } else {
                    return conversationInfo.messages[conversationInfo.messages.length - 1].content
                }
            }
        }, destConversationIndex(state) {
            return (id) => {
                let index = 0
                for (const conversation of state.conversationList) {
                    if (conversation.id == id) {
                        return index
                    }
                    index++
                }
            }
        }, getImageUrl(state) {
            return (id) => {
                for (let friend of state.contactlist.friends) {
                    if (friend.account == id) {
                        return friend.imageurl
                    }
                }
                for (let friend of state.contactlist.family) {
                    if (friend.account == id) {
                        return friend.imageurl
                    }
                }
                return state.selfAvatar
            }
        }
    },
    actions: {
        setNewFriend({commit}, newFriend) {
            commit('setNewFriend', newFriend)
        },
        changeCurrentConversationIndex({commit}, index) {
            commit('changeCurrentConversationIndex', index)
        },
        changeCurrentConversationId({commit}, id) {
            commit('changeCurrentConversationId', id)
        },
        receive({commit}, [messages, lastMessageId, userId]) {
            commit('receive', [messages, lastMessageId, userId])
        },
        initConversation({commit}, conversationList) {
            commit('initConversation', conversationList)
        },
        initLastMessageId({commit}, lastMessageId) {
            commit('initLastMessageId', lastMessageId)
        },
        initContact({commit},) {
            commit('initContact')
        },
        sendMessage({commit}, message) {
            commit('sendMessage', message)
        },
        insertNewFriend({commit}, friend) {
            commit('insertNewFriend', friend)

        },
        insertFriend({commit}, friend) {
            commit('insertFriend', friend)

        },
        insertFamily({commit}, friend) {
            commit('insertFamily', friend)

        },
        setPersonInfo({commit}, personInfo) {
            commit('setPersonInfo', personInfo)
        },
        setLocalPicture({commit}, [k, v]) {
            commit('setLocalPicture', [k, v])
        }
    },
    mutations: {
        insertNewFriend(state, friend) {
            state.contactlist.newfriends.splice(0, 0, friend)
        },
        insertFriend(state, friend) {
            state.contactlist.friends.splice(0, 0, friend)
        },
        insertFamily(state, friend) {
            state.contactlist.family.splice(0, 0, friend)
        },
        setPersonInfo(state, personInfo) {
            state.show_person_info = personInfo

        },
        setLocalPicture(state, [k, v]) {
            state.localPicture[k] = v
        },
        initContact(state) {
            state.contactlist.friends = []
            state.contactlist.group = []
            state.contactlist.family = []
        },
        setNewFriend(state, newFriend) {
            state.newfriend = newFriend
        },
        changeCurrentConversationIndex(state, index) {
            state.currentConversationIndex = index
        },
        changeCurrentConversationId(state, id) {
            let newConversation = true
            state.conversationList.forEach((conversation, index) => {
                if (conversation.id == id) {
                    state.currentConversationIndex = index
                    newConversation = false
                }
            })
            if (newConversation) {
                let title
                state.contactlist.friends.forEach(friend => {
                    if (friend.account == id) {
                        title = friend.nickname
                    }
                })
                let messages = []

                db.find({id: +id}, (err, docs) => {
                    console.log(docs)
                    if (docs.length === 0) {
                        db.insert(item)
                    } else {
                        db.update({id: +id}, {
                            title,
                            imageurl: 'image1.jpg',
                        })
                        messages = docs[0].messages
                    }
                })
                const item = {
                    type: 'conversation',
                    id: +id,
                    _id: +id,
                    title,
                    imageurl: 'image1.jpg',
                    messages: []
                }
                console.log('new conversation')
                state.conversationList.splice(0, 0, item)
            }
        },
        receive(state, [messages, lastMessageId, userId]) {
            state.lastMessageId = lastMessageId
            messages.forEach(message => {
                let id;
                if (message['senderId'] == userId) {
                    id = message['receiverId'];
                    message.left = true
                } else {
                    id = message['senderId'];
                }
                let newConversation = true
                for (let conversation of state.conversationList) {
                    if (conversation.id == id) {
                        conversation.messages.push(message)
                        newConversation = false
                        break
                    }
                }
                if (newConversation) {
                    state.conversationList.splice(0, 0, {
                        type: 'conversation',
                        id: +id,
                        title: +id,
                        imageurl: 'image1.jpg',
                        messages: [message]
                    })
                }
            })
        },
        initConversation(state, conversationList) {
            state.conversationList = conversationList
            window.startTimer()
        }
        , initLastMessageId(state, lastMessageId) {
            state.lastMessageId = lastMessageId
        }
        ,
        sendMessage(state, message) {
            const conversationInfo = state.conversationList[state.currentConversationIndex]
            conversationInfo.messages.push(message)
        }
    }
})
